using Blazorise;

using Volo.Abp.AspNetCore.Components.Web.Extensibility.EntityActions;
using Volo.Abp.AspNetCore.Components.Web.Extensibility.TableColumns;
using Volo.Abp.AspNetCore.Components.Web.Theming.PageToolbars;

using WL.Think.Abp.Localization;
using WL.Think.Abp.OpenIddict.Dtos;
using WL.Think.Abp.OpenIddict.Localization;
using WL.Think.Abp.OpenIddict.Permission;

namespace WL.Think.Abp.OpenIddict.Pages.Scopes
{
    public partial class ScopeManagement
    {
        protected string? ResourceText { get; set; }
        protected PageToolbar Toolbar { get; } = new();
        protected List<TableColumn> DataColumns => TableColumns.Get<ScopeManagement>();
        private bool IsOpenCreateModal { get; set; }

        public ScopeManagement()
        {
            CreatePolicyName = OpenIddictPermissions.Scopes.Create;
            UpdatePolicyName = OpenIddictPermissions.Scopes.Update;
            DeletePolicyName = OpenIddictPermissions.Scopes.Delete;
            LocalizationResource = typeof(ThinkResource);
        }

        /// <summary>
        /// 继承父类，用来设置工具栏按钮
        /// </summary>
        /// <returns></returns>
        protected override ValueTask SetToolbarItemsAsync()
        {
            Toolbar.AddButton(L[ThinkResourceNames.New], OpenCreateModalAsync, IconName.Add,requiredPolicyName: CreatePolicyName);
            Toolbar.AddButton(L[ThinkResourceNames.Search], GetEntitiesAsync, IconName.Search, Color.Light);
            return base.SetToolbarItemsAsync();
        }

        protected override Task OpenCreateModalAsync()
        {
            IsOpenCreateModal = true;
            return base.OpenCreateModalAsync();
        }

        protected override Task CloseCreateModalAsync()
        {
            IsOpenCreateModal = false;
            return base.CloseCreateModalAsync();
        }

        protected override ValueTask SetBreadcrumbItemsAsync()
        {
            BreadcrumbItems.Add(new Volo.Abp.BlazoriseUI.BreadcrumbItem(L["OpenIddictManagement"].Value));
            BreadcrumbItems.Add(new Volo.Abp.BlazoriseUI.BreadcrumbItem(L["Scopes"].Value));
            return base.SetBreadcrumbItemsAsync();
        }

        /// <summary>
        /// 继承父类，用来配置实体操作按钮 
        /// </summary>
        /// <returns></returns>
        protected override ValueTask SetEntityActionsAsync()
        {
            EntityActions
                .Get<ScopeManagement>()
                .AddRange([
                    new EntityAction
                    {
                        Text = L[ThinkResourceNames.Edit],
                        Icon = "fa fa-pen-to-square",
                        Clicked = async (d) => await OpenEditModalAsync(d.As<ScopeDto>()),
                        Visible = o => HasUpdatePermission
                    },
                    new EntityAction
                    {
                        Text = L[ThinkResourceNames.Delete],
                        Icon = "fa fa-delete",
                        Clicked = async (d) => await DeleteEntityAsync(d.As<ScopeDto>()),
                        Visible = o => HasDeletePermission,
                        ConfirmationMessage = (d) => GetDeleteConfirmationMessage(d.As<ScopeDto>()),
                    }
                ]);
            return base.SetEntityActionsAsync();
        }

        /// <summary>
        /// 继承父类，用来配置表格列
        /// </summary>
        /// <returns></returns>
        protected override async ValueTask SetTableColumnsAsync()
        {
            DataColumns
                .AddRange([
                    new TableColumn
                    {
                        Title = L[ThinkResourceNames.Action],
                        Actions = EntityActions.Get<ScopeManagement>(),
                    },
                    new TableColumn{
                        Title = L[ThinkResourceNames.Name],
                        Data = nameof(ScopeDto.Name),
                        PropertyName = nameof(ScopeDto.Name),
                    },
                    new TableColumn{
                        Title = L[ThinkResourceNames.DisplayName],
                        Data = nameof(ScopeDto.DisplayName),
                        PropertyName = nameof(ScopeDto.DisplayName),
                    },
                    new TableColumn{
                        Title = L[ThinkResourceNames.Description],
                        Data = nameof(ScopeDto.Description),
                        PropertyName = nameof(ScopeDto.Description),
                    },
                    new TableColumn{
                        Title = L[ThinkResourceNames.Resource],
                        Data = nameof(ScopeDto.Resources),
                        PropertyName = nameof(ScopeDto.Resources),
                        ValueConverter = item=> ((ScopeDto)item).Resources.JoinAsString(",")
                    },
                    new TableColumn{
                        Title = L[ThinkResourceNames.LastModificationTime],
                        Data = nameof(ScopeDto.LastModificationTime),
                        PropertyName = nameof(ScopeDto.LastModificationTime)
                    }
                ]);

            await base.SetTableColumnsAsync();
        }

        protected override Task GetEntitiesAsync()
        {
            return base.GetEntitiesAsync();
        }



        /// <summary>
        /// 添加资源
        /// </summary>
        /// <returns></returns>
        protected virtual async Task AddResourceAsync()
        {
            if(ResourceText.IsNullOrWhiteSpace()) return;
            if(IsOpenCreateModal)
            {
                NewEntity.Resources.Add(ResourceText);
            }
            else {
                EditingEntity.Resources.Add(ResourceText);
            }
            ResourceText = "";
            await Task.CompletedTask;
        }
    }
}
